
module Admin::ApplicationHelper

  DYNAMIC_ACTIONS = {
    "update": "编辑",
    "read": "查看"
  }
  DYNAMIC_ITEMS = {
    "product": "产品",
    "order": "用户及订单"
  }

  def self.included(base)
    dynamic_create_role_action
  end

  def role_action role
    case role.name
    when 'admin'
      "所有网站后台权限"
    when 'guest'
      "所有商城权限"
    else
      role.role_resources.map do |resource|
        send(resource.resource_name)
      end.join("，")
    end
  end
  
  def convert_promotion_date datetime
    if datetime.blank?
      Time.now.to_date.to_s(:db)
    else
      datetime.to_date.to_s(:db)
    end
  end


  private

  def self.dynamic_create_role_action
    DYNAMIC_ACTIONS.each do |key, value|
      DYNAMIC_ITEMS.each do |inner_key, inner_value|
        define_method "#{key}_#{inner_key}" do
          DYNAMIC_ACTIONS[key].to_s + DYNAMIC_ITEMS[inner_key].to_s
        end
      end
    end
  end

end
